/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ConfirmaPagamentoJIntFr.java
 *
 * Created on 08/11/2009, 15:54:38
 */
package br.com.actionsystem.gdp.telas;

import br.com.actionsystem.gdp.dao.IntPagamentoDao;
import br.com.actionsystem.gdp.dao.IntReservasDao;
import br.com.actionsystem.gdp.dao.PagamentoDaoImpl;
import br.com.actionsystem.gdp.dao.ReservaDao;
import br.com.actionsystem.gdp.modelo.Despesa;
import br.com.actionsystem.gdp.modelo.Historico;
import br.com.actionsystem.gdp.modelo.Receita;
import br.com.actionsystem.gdp.modelo.Reservas;
import br.com.actionsystem.gdp.util.DaoException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.swing.JOptionPane;

/**
 *
 * @author moises
 */
public class ConfirmaPagamentoJIntFr extends javax.swing.JInternalFrame {

    private Historico historico;
    private Historico ultimoHistorico;
    private Historico historicoAux = new Historico();
    private SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
    private List<Reservas> listReservas;
    private IntReservasDao daoReserva = new ReservaDao();
    private IntPagamentoDao daoPag = new PagamentoDaoImpl();
    private Reservas reserv = new Reservas();
    private Pagamentos pagamentos;

    /** Creates new form ConfirmaPagamentoJIntFr */
    public ConfirmaPagamentoJIntFr(Pagamentos pag) {
        this();
        this.pagamentos = pag;
    }

    public ConfirmaPagamentoJIntFr() {
        initComponents();
        this.reservasComboBox.setToolTipText("Sua lista de Reservas");
        this.btCancelar.setToolTipText("Cancela opereção");
        this.tbExcluir.setToolTipText("Excluir histórico");
        this.setLocation(200, 200);
    }

    public void preencheTela(Historico hist) {
        this.historico = hist;
        //Configura tela que confirma o pagamento ou recebimento
        if (hist.getAtividadeFK() instanceof Receita) {
            this.setTitle("Confirma Recebimento");
            this.btConfirmar.setText("Receber");
            this.jlabelSinal.setText("+");
            this.valorTextField1.setToolTipText("Valor à receber");
            this.btConfirmar.setToolTipText("Confirmar o recebimento");
        } else if (hist.getAtividadeFK() instanceof Despesa) {
            this.setTitle("Confirma Pagamento");
            this.btConfirmar.setText(" Pagar ");
            this.jlabelSinal.setText("-");
            this.valorTextField1.setToolTipText("Valor à pagar");
            this.btConfirmar.setToolTipText("Confirma o pagamento");
        }
        jLabelDescricao.setText(historico.getAtividadeFK().getDescricao());
        jLabelDataVencimento.setText(sdf.format(historico.getDataPagamento()));
        jLabelDataPagamento.setText(sdf.format(new Date()));
        jLabelNumeroParcela.setText("" + historico.getNumeroParcela());

        jLabelCategoria.setText(historico.getAtividadeFK().getCategoria().getDescricao());

        reservasComboBox.removeAllItems();
        listReservas = daoReserva.listaTodas();

        for (Reservas r : listReservas) {
            reservasComboBox.addItem(r);
        }

        //Ordenar lista comboBox
        jLabelSaldo.setText("" + ((Reservas) reservasComboBox.getSelectedItem()).getSaldo());
        valorTextField1.setText("" + historico.getValor());
        reserv = (Reservas) reservasComboBox.getSelectedItem();

        reservasComboBox.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                reserv = (Reservas) reservasComboBox.getSelectedItem();
                if (reserv != null) {
                    jLabelSaldo.setText("" + reserv.getSaldo());
                }
                jLabelSaldo.repaint();
            }
        });

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        dadosPagamentojPanel1 = new javax.swing.JPanel();
        jdescricao = new javax.swing.JLabel();
        jdataPagamento = new javax.swing.JLabel();
        valorReal = new javax.swing.JLabel();
        jnumeroParcela = new javax.swing.JLabel();
        jcategoria = new javax.swing.JLabel();
        jreserva = new javax.swing.JLabel();
        jdataVencimento = new javax.swing.JLabel();
        jsaldo = new javax.swing.JLabel();
        reservasComboBox = new javax.swing.JComboBox();
        valorTextField1 = new javax.swing.JTextField();
        btConfirmar = new javax.swing.JButton();
        btCancelar = new javax.swing.JButton();
        jLabelDescricao = new javax.swing.JLabel();
        jLabelDataVencimento = new javax.swing.JLabel();
        jLabelDataPagamento = new javax.swing.JLabel();
        jLabelNumeroParcela = new javax.swing.JLabel();
        jLabelCategoria = new javax.swing.JLabel();
        jLabelSaldo = new javax.swing.JLabel();
        jlabelSinal = new javax.swing.JLabel();
        tbExcluir = new javax.swing.JButton();

        setTitle("Confirmando Pagamento");

        dadosPagamentojPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        dadosPagamentojPanel1.setMaximumSize(new java.awt.Dimension(442, 219));
        dadosPagamentojPanel1.setMinimumSize(new java.awt.Dimension(442, 219));

        jdescricao.setText("Descrição:");

        jdataPagamento.setText("Data do Pagamento:");

        valorReal.setText("Valor Real:");

        jnumeroParcela.setText("Numero da Parcela:");

        jcategoria.setText("Categoria:");

        jreserva.setText("Reserva:");

        jdataVencimento.setText("Data do Vencimento:");

        jsaldo.setText("Saldo:");

        btConfirmar.setText("Confirma");
        btConfirmar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btConfirmarActionPerformed(evt);
            }
        });

        btCancelar.setText("Cancelar");
        btCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeActionPerformed(evt);
            }
        });

        jlabelSinal.setForeground(new java.awt.Color(255, 0, 0));

        tbExcluir.setText("Excluir");
        tbExcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tbExcluirActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout dadosPagamentojPanel1Layout = new org.jdesktop.layout.GroupLayout(dadosPagamentojPanel1);
        dadosPagamentojPanel1.setLayout(dadosPagamentojPanel1Layout);
        dadosPagamentojPanel1Layout.setHorizontalGroup(
            dadosPagamentojPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(dadosPagamentojPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(dadosPagamentojPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(dadosPagamentojPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(dadosPagamentojPanel1Layout.createSequentialGroup()
                            .add(jdescricao)
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                            .add(jLabelDescricao)
                            .addContainerGap())
                        .add(org.jdesktop.layout.GroupLayout.TRAILING, dadosPagamentojPanel1Layout.createSequentialGroup()
                            .add(dadosPagamentojPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                .add(dadosPagamentojPanel1Layout.createSequentialGroup()
                                    .add(jdataVencimento)
                                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                    .add(jLabelDataVencimento))
                                .add(dadosPagamentojPanel1Layout.createSequentialGroup()
                                    .add(jreserva)
                                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                    .add(reservasComboBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 140, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                                .add(dadosPagamentojPanel1Layout.createSequentialGroup()
                                    .add(jnumeroParcela)
                                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                    .add(jLabelNumeroParcela)))
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 6, Short.MAX_VALUE)
                            .add(dadosPagamentojPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                .add(dadosPagamentojPanel1Layout.createSequentialGroup()
                                    .add(jdataPagamento)
                                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                    .add(jLabelDataPagamento))
                                .add(dadosPagamentojPanel1Layout.createSequentialGroup()
                                    .add(jcategoria)
                                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                    .add(jLabelCategoria))
                                .add(dadosPagamentojPanel1Layout.createSequentialGroup()
                                    .add(dadosPagamentojPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                        .add(org.jdesktop.layout.GroupLayout.TRAILING, dadosPagamentojPanel1Layout.createSequentialGroup()
                                            .add(valorReal)
                                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED))
                                        .add(dadosPagamentojPanel1Layout.createSequentialGroup()
                                            .add(jsaldo)
                                            .add(32, 32, 32)))
                                    .add(dadosPagamentojPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                        .add(jLabelSaldo)
                                        .add(valorTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 103, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                        .add(jlabelSinal))))
                            .add(61, 61, 61)))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, dadosPagamentojPanel1Layout.createSequentialGroup()
                        .add(btConfirmar)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(tbExcluir, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 72, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(btCancelar)
                        .add(69, 69, 69))))
        );
        dadosPagamentojPanel1Layout.setVerticalGroup(
            dadosPagamentojPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(dadosPagamentojPanel1Layout.createSequentialGroup()
                .add(dadosPagamentojPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jdescricao)
                    .add(jLabelDescricao))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(dadosPagamentojPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jdataVencimento)
                    .add(jdataPagamento)
                    .add(jLabelDataVencimento)
                    .add(jLabelDataPagamento))
                .add(dadosPagamentojPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(dadosPagamentojPanel1Layout.createSequentialGroup()
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(dadosPagamentojPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jnumeroParcela)
                            .add(jcategoria)
                            .add(jLabelNumeroParcela)
                            .add(jLabelCategoria))
                        .add(26, 26, 26)
                        .add(dadosPagamentojPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jreserva)
                            .add(reservasComboBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(jsaldo)
                            .add(jLabelSaldo))
                        .add(18, 18, 18))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, dadosPagamentojPanel1Layout.createSequentialGroup()
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(jlabelSinal)
                        .add(6, 6, 6)))
                .add(dadosPagamentojPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(valorReal)
                    .add(valorTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(dadosPagamentojPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(btCancelar)
                    .add(tbExcluir)
                    .add(btConfirmar))
                .addContainerGap(20, Short.MAX_VALUE))
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(18, 18, 18)
                .add(dadosPagamentojPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 431, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(dadosPagamentojPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 196, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(15, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void closeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_closeActionPerformed

    private void btConfirmarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btConfirmarActionPerformed
        //se permanente, criar outro historico
        if (historico.getAtividadeFK().getNumParcela() == 0) {
            try {
                ultimoHistorico = daoPag.getMaxHistorico(historico.getAtividadeFK());
                historicoAux = this.incrementaUmMes(ultimoHistorico);
            } catch (DaoException ex) {
                ex.printStackTrace();
            }
        }

        //atualiza o valor do saldo
        if (historico.getAtividadeFK() instanceof Receita) {
            reserv.setSaldo(reserv.getSaldo() + (Float.parseFloat(valorTextField1.getText())));
        } else if (historico.getAtividadeFK() instanceof Despesa) {
            reserv.setSaldo(reserv.getSaldo() - (Float.parseFloat(valorTextField1.getText())));
        }

        //seta historico como pago
        historico.setIsPago(true);

        try {
            daoPag.salvaPagamento(historico);
            daoPag.salvaPagamento(historicoAux);
            daoReserva.salvar(reserv);
        } catch (DaoException ex) {
            ex.printStackTrace();
        } finally {
            this.setVisible(false);
            if(this.pagamentos != null){
                this.pagamentos.atualizaTabela();
            }
        }
    }//GEN-LAST:event_btConfirmarActionPerformed

    public Historico incrementaUmMes(Historico h) {
        Historico hs = new Historico();
        Calendar c = Calendar.getInstance();
        c.setTime(h.getDataPagamento());
        c.add(Calendar.MONTH, 1);
        hs.setIdAtividadeFK(h.getAtividadeFK());
        hs.setValor(h.getValor());
        hs.setNumeroParcela(h.getNumeroParcela() + 1);
        hs.setIsPago(false);
        hs.setDataPagamento(c.getTime());
        return hs;
    }

    private void tbExcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tbExcluirActionPerformed
        Object opcoes[] = {"Sim", "Não"};
        int op = JOptionPane.showInternalOptionDialog(ConfirmaPagamentoJIntFr.this,
                "Você deseja realmente apagar esse Historico?", "Aviso",
                JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, opcoes, null);
        
        if (op == 0) {
            try {
                if (historico.getAtividadeFK().getNumParcela() == 0) {
                    ultimoHistorico = daoPag.getMaxHistorico(historico.getAtividadeFK());
                    historicoAux = this.incrementaUmMes(ultimoHistorico);
                    daoPag.salvaPagamento(historicoAux);
                }
                daoPag.deleteHistorico(historico);
            } catch (DaoException ex) {
                ex.printStackTrace();
            } finally {
                this.setVisible(false);
                this.pagamentos.atualizaTabela();
            }
        }
    }//GEN-LAST:event_tbExcluirActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btCancelar;
    private javax.swing.JButton btConfirmar;
    private javax.swing.JPanel dadosPagamentojPanel1;
    private javax.swing.JLabel jLabelCategoria;
    private javax.swing.JLabel jLabelDataPagamento;
    private javax.swing.JLabel jLabelDataVencimento;
    private javax.swing.JLabel jLabelDescricao;
    private javax.swing.JLabel jLabelNumeroParcela;
    private javax.swing.JLabel jLabelSaldo;
    private javax.swing.JLabel jcategoria;
    private javax.swing.JLabel jdataPagamento;
    private javax.swing.JLabel jdataVencimento;
    private javax.swing.JLabel jdescricao;
    private javax.swing.JLabel jlabelSinal;
    private javax.swing.JLabel jnumeroParcela;
    private javax.swing.JLabel jreserva;
    private javax.swing.JLabel jsaldo;
    private javax.swing.JComboBox reservasComboBox;
    private javax.swing.JButton tbExcluir;
    private javax.swing.JLabel valorReal;
    private javax.swing.JTextField valorTextField1;
    // End of variables declaration//GEN-END:variables
}
